package com.info.zhiduoduo.core.service.impl;

import com.info.zhiduoduo.core.service.CoreSensitiveService;
import com.info.zhiduoduo.repository.mapper.system.SensitiveVocabularyMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author tianyonghong
 * @create 2022/3/3
 */
@Service
public class CoreSensitiveServiceImpl implements CoreSensitiveService {

    @Resource
    private SensitiveVocabularyMapper sensitiveVocabularyMapper;

    /**
     * 检查是否存在敏感词
     * @param sensitiveWords
     * @return
     */
    @Override
    public String checkSensitiveWords(String... sensitiveWords) {
        return checkSensitiveWords(Arrays.asList(sensitiveWords));
    }

    @Override
    public String checkSensitiveWords(List<String> sensitiveWords) {
        Set<String> set = new HashSet<>();
        for (String word : sensitiveWords) {
            if(StringUtils.isNotBlank(word)) {
                String result = sensitiveVocabularyMapper.checkSensitiveWord(word);
                if(StringUtils.isNotBlank(result)) {
                    set.add(result);
                }
            }
        }
        if (CollectionUtils.isNotEmpty(set)) {
            return StringUtils.join(set, "、");
        }
        return null;
    }

}
